package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.OrderDetial;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.OrderEditerView;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/24 0024.
 */

public class OrderEditerPersenter extends BasePresent<OrderEditerView> {

    private final String TAG = "OrderEditerPersenter";

    RetrofitSerives retrofitSerives;
    Call<ResultMessage> addOrderCall;
    Call<OrderDetial> orderDetialCall;

    /**
     * 订单详情
     *
     * @param jsonParams
     */
    public void getCheckOrder(String jsonParams) {

        view.showProgress();
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        orderDetialCall = retrofitSerives.getCheckOrder(body);
        orderDetialCall.enqueue(new Callback<OrderDetial>() {
            @Override
            public void onResponse(Call<OrderDetial> call, Response<OrderDetial> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        if (response.body().getResult() != null) {
                            view.setOrderDetial(response.body());
                        } else {
                            view.showNullLayout();
                        }
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<OrderDetial> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    /**
     * 编辑订单
     *
     * @param jsonParams
     */
    public void getEditOrder(String jsonParams) {
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        addOrderCall = retrofitSerives.getEditOrder(body);
        addOrderCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setCommitOrder();
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    /**
     * 创建订单
     *
     * @param jsonParams
     */
    public void getCreateOrder(String jsonParams) {
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        addOrderCall = retrofitSerives.getAddOrder(body);
        addOrderCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setCommitOrder();
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    @Override
    public void stopRequest() {

    }
}
